package com.yunatt.service.impl;

import com.yunatt.mapper.MachineCommandMapper;
import com.yunatt.mapper.PersonMapper;
import com.yunatt.model.EnrollInfoEntity;
import com.yunatt.model.MachineCommandEntity;
import com.yunatt.model.PersonEntity;
import com.yunatt.model.vo.SendMessage;
import com.yunatt.model.vo.UserInfo;
import com.yunatt.service.EnrollInfoService;
import com.yunatt.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PersonServiceImpl implements PersonService {
    @Autowired
    private PersonMapper personMapper;

    @Autowired
    private EnrollInfoService enrollInfoService;

    @Autowired
    private MachineCommandMapper machineCommandMapper;

    @Override
    public void deleteUserInfoFromDevice(Long enrollId, String deviceSn) {
        int backupnum=13;
        String message="{\"cmd\":\"deleteuser\",\"enrollid\":"+enrollId+",\"backupnum\":"+backupnum+"}";

        MachineCommandEntity machineCommand=new MachineCommandEntity();
        machineCommand.setContent(message);
        machineCommand.setName("deleteuser");
        machineCommand.setStatus(0);
        machineCommand.setSendStatus(0);
        machineCommand.setErrCount(0);
        machineCommand.setSerial(deviceSn);
        machineCommand.setGmtCrate(new Date());
        machineCommand.setGmtModified(new Date());

        machineCommandMapper.insert(machineCommand);
        personMapper.deleteById(enrollId);
        personMapper.deleteByEnrollId(enrollId);

    }


    @Override
    public int insert(PersonEntity record) {
        return personMapper.insert(record);
    }

    @Override
    public PersonEntity selectByPrimaryKey(Long id) {
        return personMapper.selectById(id);
    }

    @Override
    public int updateByPrimaryKey(PersonEntity record) {
        return personMapper.updateById(record);
    }
    @Override
    public List<PersonEntity> selectAll() {
        // TODO Auto-generated method stub
        return personMapper.selectAll();
    }

    @Override
    public void getSignature2(List<EnrollInfoEntity> enrolls, String deviceSn) {
        for (int i = 0; i < enrolls.size(); i++) {

            String message1="{\"cmd\":\"getuserinfo\",\"enrollid\":"+enrolls.get(i).getEnrollId()+",\"backupnum\":"+enrolls.get(i).getBackupnum()+"}";
            //	String message="{\"cmd\":\"getuserinfo\",\"enrollid\":"+enrollId+",\"backupnum\":"+ backupNum+"}";
            MachineCommandEntity machineCommand=new MachineCommandEntity();
            machineCommand.setContent(message1);
            machineCommand.setName("getuserinfo");
            machineCommand.setStatus(0);
            machineCommand.setSendStatus(0);
            machineCommand.setErrCount(0);
            machineCommand.setSerial(deviceSn);
            machineCommand.setGmtCrate(new Date());
            machineCommand.setGmtModified(new Date());
            machineCommandMapper.insert(machineCommand);
        }
    }

    @Override
    public void setUserToDevice2(String deviceSn) {
        List<UserInfo>userInfos=enrollInfoService.usersToSendDevice();

        System.out.println(userInfos.size());
        for (int i = 0; i < userInfos.size(); i++) {
            Long enrollId=userInfos.get(i).getEnrollId();
            String name=userInfos.get(i).getName();
            int backupnum=userInfos.get(i).getBackupnum();
            int admin=userInfos.get(i).getAdmin();
            String record=userInfos.get(i).getRecord();
            SendMessage message=new SendMessage();
            MachineCommandEntity machineCommand=new MachineCommandEntity();

            machineCommand.setName("setuserinfo");
            machineCommand.setStatus(0);
            machineCommand.setSendStatus(0);
            machineCommand.setErrCount(0);
            machineCommand.setSerial(deviceSn);
            machineCommand.setGmtCrate(new Date());
            machineCommand.setGmtModified(new Date());


            machineCommand.setContent("{\"cmd\":\"setuserinfo\",\"enrollid\":"+enrollId+ ",\"name\":\"" + name +"\",\"backupnum\":" + backupnum
                    + ",\"admin\":" + admin + ",\"record\":\"" + record + "\"}");

            if (backupnum==11||backupnum==10) {
                machineCommand.setContent("{\"cmd\":\"setuserinfo\",\"enrollid\":"+enrollId+ ",\"name\":\"" + name +"\",\"backupnum\":" + backupnum
                        + ",\"admin\":" + admin + ",\"record\":" + record + "}");
            }

            machineCommandMapper.insert(machineCommand);


        }
    }


    @Override
    public void setUsernameToDevice(String deviceSn) {
        List<PersonEntity>persons=personMapper.selectAll();

        System.out.println(persons.size());
        int i=0;
        StringBuilder sb=new StringBuilder();
        sb.append("{\"cmd\":\"setusername\",\"count\":"+persons.size()+",\"record\":[");
        for (int j = 0; j < persons.size(); j++) {
            if(j==persons.size()-1||persons.size()==1){
                sb.append("{\"enrollid\":"+persons.get(j).getId()+",\"name\":\"" + persons.get(j).getName()+"\"}");
            }else{
                sb.append("{\"enrollid\":"+persons.get(j).getId()+",\"name\":\"" + persons.get(j).getName()+"\"},");
            }
        }
        sb.append("]}");
        System.out.println("Issue user name"+sb);
        MachineCommandEntity machineCommand=new MachineCommandEntity();

        machineCommand.setName("setusername");
        machineCommand.setStatus(0);
        machineCommand.setSendStatus(0);
        machineCommand.setErrCount(0);
        machineCommand.setSerial(deviceSn);
        machineCommand.setGmtCrate(new Date());
        machineCommand.setGmtModified(new Date());
        machineCommand.setContent(sb.toString());
        machineCommandMapper.insert(machineCommand);
    }

    @Override
    public void setUserToDevice(Long enrollId,String name,int backupnum,int admin,String records,String deviceSn) {
        if(backupnum!=-1) {


            MachineCommandEntity machineCommand=new MachineCommandEntity();

            machineCommand.setName("setuserinfo");
            machineCommand.setStatus(0);
            machineCommand.setSendStatus(0);
            machineCommand.setErrCount(0);
            machineCommand.setSerial(deviceSn);
            machineCommand.setGmtCrate(new Date());
            machineCommand.setGmtModified(new Date());

            machineCommand.setContent("{\"cmd\":\"setuserinfo\",\"enrollid\":"+enrollId+ ",\"name\":\"" + name +"\",\"backupnum\":" + backupnum
                    + ",\"admin\":" + admin + ",\"record\":\"" + records + "\"}");;

            if (backupnum==11||backupnum==10) {
                machineCommand.setContent("{\"cmd\":\"setuserinfo\",\"enrollid\":"+enrollId+ ",\"name\":\"" + name +"\",\"backupnum\":" + backupnum
                        + ",\"admin\":" + admin + ",\"record\":" + records + "}");
            }


            machineCommandMapper.insert(machineCommand);
        }else {

            StringBuilder sb=new StringBuilder();
            sb.append("{\"cmd\":\"setusername\",\"count\":"+"1"+",\"record\":[");

            sb.append("{\"enrollid\":"+enrollId+",\"name\":\"" + name+"\"}");

            sb.append("]}");
            System.out.println("Issue user name"+sb);
            MachineCommandEntity machineCommand=new MachineCommandEntity();

            machineCommand.setName("setusername");
            machineCommand.setStatus(0);
            machineCommand.setSendStatus(0);
            machineCommand.setErrCount(0);
            machineCommand.setSerial(deviceSn);
            machineCommand.setGmtCrate(new Date());
            machineCommand.setGmtModified(new Date());
            machineCommand.setContent(sb.toString());
            machineCommandMapper.insert(machineCommand);


        }
    }
}
